#include<iostream>
#include<vector>
#include<queue>
#include<utility>
using namespace std;

class Solution {
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int m, n;
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if (image[sr][sc] == color) return image;
        m = image.size(), n = image[0].size();
        int pre = image[sr][sc];
        queue<pair<int, int>> q;
        q.push({ sr,sc });
        while (q.size())
        {
            int a = q.front().first;
            int b = q.front().second;

            q.pop();

            image[a][b] = color;
            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == pre)
                {
                    q.push({ x,y });
                }
            }
        }
        return image;
    }


};